package com.boot2.core.utils;


import org.apache.commons.jexl2.JexlEngine;
import org.jxls.common.Context;
import org.jxls.expression.JexlExpressionEvaluator;
import org.jxls.transform.Transformer;
import org.jxls.util.JxlsHelper;

import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangweilin
 * @date 2019/6/13 16:03
 * @Description: 导出excel
 */
public class ExcelUtils {
    /**
     * 读取模板文件导出到文件
     *
     * @param list
     * @param template
     * @param destFile
     * @throws Exception
     */
    public static void writeXlsFile(List<?> list, String template, String destFile) throws Exception {
//        File templateFile = ResourceUtil.getResourceFile(template);
//        InputStream inputStream = new FileInputStream(templateFile);
        InputStream inputStream = ResourceUtil.getInputStream(template);
        OutputStream outputStream = new FileOutputStream(destFile);
        exportFile(list, inputStream, outputStream);
    }

    /**
     * 读取模板文件导出到输出流
     *
     * @param list
     * @param template
     * @param os
     * @throws Exception
     */
    public static void exportFile(List<?> list, String template, OutputStream outputStream) throws Exception {
//        File templateFile = ResourceUtil.getResourceFile(template);
//        InputStream inputStream = new FileInputStream(templateFile);
        InputStream inputStream = ResourceUtil.getInputStream(template);
        exportFile(list, inputStream, outputStream);
    }

    /**
     * 读取模板流导出到文件
     *
     * @param list
     * @param template
     * @param destFile
     * @throws Exception
     */
    public static void writeXlsFile(List<?> list, InputStream inputStream, String destFile) throws Exception {
        OutputStream outputStream = new FileOutputStream(destFile);
        exportFile(list, inputStream, outputStream);
    }

    /**
     * 读取模板流导出到输出流
     *
     * @param list
     * @param template
     * @param outputStream
     * @throws Exception
     */
    public static void exportFile(List<?> list, InputStream inputStream, OutputStream outputStream) throws Exception {
        try {
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("count", list.size());
            dataMap.put("dataList", list);
            Context context = new Context(dataMap);
            JxlsHelper jxlsHelper = JxlsHelper.getInstance();
            Transformer transformer = jxlsHelper.createTransformer(inputStream, outputStream);
            JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator) transformer.getTransformationConfig().getExpressionEvaluator();
            Map<String, Object> funcsMap = new HashMap<String, Object>();
            funcsMap.put("fn", new ExcelUtils());    //添加自定义功能
            JexlEngine jexlEngine = evaluator.getJexlEngine();
            jexlEngine.setFunctions(funcsMap);
            jxlsHelper.processTemplate(context, transformer);
        } finally {
            inputStream.close();
            outputStream.flush();
            outputStream.close();
        }
    }


    // 日期格式化
    public String formatDate(Date date, String pattern) {
        return DateUtil.formatDate(date, pattern);
    }

    // if判断
    public Object ifelse(boolean b, Object o1, Object o2) {
        return b ? o1 : o2;
    }

//    /**
//     * 获取字符串类型内容
//     *
//     * @param cell
//     * @return
//     * @throws Exception
//     */
//    public static String cellToString(Cell cell) throws Exception
//    {
//        if (!StringUtils.isEmpty(cell))
//        {
//            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
//            if (!StringUtils.isEmpty(cell.toString().trim()))
//            {
//                return cell.toString().trim();
//            }
//        }
//        return null;
//    }
}
